Skip to content

Method: resolveValue(Statement, Assertion)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j.util;
19:
20: import cz.cvut.kbss.ontodriver.model.*;
21: import org.eclipse.rdf4j.model.IRI;
22: import org.eclipse.rdf4j.model.Statement;
23:
24: import java.util.Map;
25: import java.util.Optional;
26:
27: public class AxiomBuilder {
28:
29: private final NamedResource subject;
30: private final Map<IRI, Assertion> propertyToAssertion;
31:
32: private final Assertion unspecifiedProperty;
33:
34: public AxiomBuilder(NamedResource subject, Map<IRI, Assertion> propertyToAssertion, Assertion unspecifiedProperty) {
35: this.subject = subject;
36: this.propertyToAssertion = propertyToAssertion;
37: this.unspecifiedProperty = unspecifiedProperty;
38: }
39:
40: public Axiom<?> statementToAxiom(Statement statement) {
41: final Assertion assertion = resolveAssertion(statement.getPredicate());
42:
43: return statementToAxiom(statement, assertion);
44: }
45:
46: private static Optional<Value<?>> resolveValue(Statement stmt, Assertion assertion) {
47:• if (assertion == null || Rdf4jUtils.isBlankNode(stmt.getObject())) {
48: return Optional.empty();
49: }
50: return createValue(assertion, stmt.getObject());
51: }
52:
53: public Axiom<?> statementToAxiom(Statement statement, Assertion assertion) {
54: final Optional<Value<?>> val = resolveValue(statement, assertion);
55: return val.map(v -> new AxiomImpl<>(subject, assertion, v)).orElse(null);
56: }
57:
58: private Assertion resolveAssertion(IRI predicate) {
59: Assertion assertion = propertyToAssertion.get(predicate);
60: if (assertion == null) {
61: if (unspecifiedProperty != null) {
62: assertion = Assertion
63: .createPropertyAssertion(Rdf4jUtils.toJavaUri(predicate), unspecifiedProperty.isInferred());
64: }
65: } else if (assertion.getType() == Assertion.AssertionType.PROPERTY) {
66: // If the property was unspecified, create assertion based on the actual property URI
67: assertion = Assertion.createPropertyAssertion(Rdf4jUtils.toJavaUri(predicate), assertion.isInferred());
68: }
69: return assertion;
70: }
71:
72: private static Optional<Value<?>> createValue(Assertion assertion, org.eclipse.rdf4j.model.Value value) {
73: return ValueConverter.fromRdf4jValue(assertion, value).map(Value::new);
74: }
75: }